//
//  HYUserCenterVC.m
//  TJHYHardwareProduct
//
//  Created by yc on 2017/12/25.
//  Copyright © 2017年 Cong Yao. All rights reserved.
//

#import "HYUserCenterVC.h"
#import "LKChangePhoneVC.h"
#import "LKChangePasswordVC.h"
#import "HYLogoutApi.h"
#import "AlertViewManager.h"
#import "HYUserInfoApi.h"

@interface HYUserCenterVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIImageView *headImageView;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *dataSource;
@property(nonatomic,copy)NSString *userName;
@property(nonatomic,copy)NSString *phone;


@end

@implementation HYUserCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self updateTableView];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)updateTableView
{
    [HYUserInfoApi getUserInfo:^(HYUserInfoModel *model) {
        self.userName = model.username;
        self.phone = model.phone;
        [self.tableView reloadData];
    } fail:^(NSDictionary *dic) {
        
    }];
}

- (void)base_loadData
{
//    self.userName = EXRACT_OBJECT(USERNAME);
//    self.phone = EXRACT_OBJECT(PHONE);

}

#pragma mark - TableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = self.dataSource.firstObject;
    return array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(200);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UserCenterCell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = fontWithSize(15);
    cell.detailTextLabel.font = fontWithSize(15);
    if (indexPath.row == 0)
    {
        cell.accessoryView = [UIView new];
    }


    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *imageArray = self.dataSource.firstObject;
    NSArray *titleArray = self.dataSource.lastObject;
    cell.imageView.image = imageWithName(imageArray[indexPath.row]);
    cell.textLabel.text = titleArray[indexPath.row];
    if (indexPath.row == 0)
    {
        cell.detailTextLabel.text = self.userName;
    }
    else if (indexPath.row == 1)
    {
        cell.detailTextLabel.text = self.phone;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row)
    {
        case 0:
            
            break;
        case 1:
        {
            LKChangePhoneVC *vc = [LKChangePhoneVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            LKChangePasswordVC *vc = [LKChangePasswordVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            [[AlertViewManager shareManager] initWithTitle:@"提示" message:@"您确定要退出登录？" cancleButtonTitle:@"取消" OtherButtonsArray:@[@"确定"] clickAtIndex:^(NSInteger buttonIndex, UIAlertController *alertController) {
                if (buttonIndex == 1)
                {
                    [HYLogoutApi logout:^(NSDictionary *dic) {
                        SAVE_OBJECT(@"NO", LOGIN);
                        REMOVE_OBJECT(TOKEN);
                        REMOVE_OBJECT(UID);
                        [SVProgressHUD showSuccessWithStatus:@"退出成功"];
                        [[AppDelegate defaultService]setRootViewController];
                    } fail:^(NSDictionary *dic) {
                        [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
                    }];
                }
            }];
        }

            break;
            
        default:
            break;
    }
}

#pragma mark - 懒加载
- (UIImageView *)headImageView
{
    if(!_headImageView)
    {
        UIView *superView = self.view;
        _headImageView = [[UIImageView alloc]init];
        [superView addSubview:_headImageView];
        _headImageView.image = [UIImage imageNamed:@"user_center_head"];
    }
    return _headImageView;
}
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]init];
        [superView addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.layer.cornerRadius = px1920Height(60)/2;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (NSArray *)dataSource
{
    if (!_dataSource)
    {
        _dataSource = @[@[@"icon_User name",@"icon_phone",@"icon_code",@"Sign out"],@[@"用户名",@"手机号码",@"修改密码",@"退出登录"]];
    }
    return _dataSource;
}


#pragma mark - 初始化视图
- (void)base_masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(585));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headImageView.mas_bottom).with.offset(-px1920Height(60));
        make.left.right.bottom.mas_equalTo(superView);
    }];
    
    [self.view bringSubviewToFront:self.navigationBar];
    
    [self.view bringSubviewToFront:self.tableView];
}





#pragma mark - 导航头设置
- (void)base_configNavigationBar
{
    [self setNavigationBarTitle:@"个人中心"];
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark - Actions Method


#pragma mark -
#pragma mark - Notification


#pragma mark -
#pragma mark - DelegateName


#pragma mark -
#pragma mark - Setter Method


#pragma mark -
#pragma mark - Data_Handlers


#pragma mark -
#pragma mark - SubViews Manager


#pragma mark -
#pragma mark - Constraint


#pragma mark -
#pragma mark - Lazy Load


@end
